<?php
class OauthController extends Zend_Controller_Action {

	protected $oauth;
	protected $auth;

	public function init() {
		$this->log = Zend_Registry::get('log');
		$this->log->debug('This is smaple log from OAuth Controller');

		//$this->oauth = new Macys_Oauth_Oauth();
		$this->oauth = new Macys_Oauth_Oauth2();
		//$this->oauth = 'test';
		$this->auth = Zend_Auth::getInstance();
	}

	public function indexAction() {
		echo "This is OAuth Sample for WSSG";
		$this->log->debug('OAuth Index Executed');
		$this->getHelper('viewRenderer')->setNoRender();
	}

	public function tokenauthAction() {
		if ($this->auth->hasIdentity()) {
			$this->log->debug(" User has Identity");
			$this->_helper->redirector('profile', 'oauth');
		}
		$request = $this->getRequest();
		$params = $request->getParams();
		$this->oauth->setResponse($params);
		$res = $this->oauth->doAuthentication('token');
		if ($res->isValid()) {
			//$this->_helper->redirector('index', 'user');
			// By Default we will be redirected to the requested path
		}
		else {
			var_dump($res);
		}
		$this->getHelper('viewRenderer')->setNoRender();
	}

	public function codeauthAction() {
		if ($this->auth->hasIdentity()) {
			$this->log->debug(" User has Identity");
			$this->_helper->redirector('profile', 'oauth');
		}
		$request = $this->getRequest();
		$params = $request->getParams();
		$this->oauth->setResponse($params);
		$res = $this->oauth->doAuthentication('code');
		if ($res->isValid()) {
			//$this->_helper->redirector('index', 'user');
		}
		else {
			var_dump($res);
		}
		$this->getHelper('viewRenderer')->setNoRender();

	}


	public function profileAction() {
		$x = Zend_Auth::getInstance();
		echo $x->getIdentity();
		echo "</br>User Authenticated";
		$this->getHelper('viewRenderer')->setNoRender();
	}


	public function refreshAction() {
		$this->oauth->refreshToken();
		echo "</br></br> Token Refreshed </br>";
		echo "</br> New Token: </br>";
		$this->dumpSess();
		$this->getHelper('viewRenderer')->setNoRender();
	}

	public function mobileauthAction() {
		if ($this->auth->hasIdentity()) {
			$this->log->debug(" User has Identity");
			$this->_helper->redirector('profile', 'oauth');
		}
		$form = new Macys_Form_MobileLogin();
		if ($this->_request->isPost()) {
			$formData = $this->_request->getPost();
			if ($form->isValid($formData)) {
				$this->oauth->setResponse($formData);
				$res = $this->oauth->doAuthentication('mobile');
				if ($res->isValid()) {
					//$this->_forward('index', 'user');
				}
				else {
					var_dump($res);
				}
			} else {
				$form->populate($formData);
			}
		}
		$this->view->form = $form;
	}


	/**
	 * Redirects the page to Macys Browser for logout
	 * Get the Users logout from browser Removes Macys Cookies
	 */
	public function blogoutAction() {
		$this->oauth->doLogout();
		$this->getHelper('viewRenderer')->setNoRender();
	}

	/**
	 * Removes the Identity info from Zend App
	 * Customers can use this logout
	 */
	public function logoutAction() {
		$this->oauth->doLogout('mobile');
		$this->_helper->redirector('index', 'oauth');
		//$this->getHelper('viewRenderer')->setNoRender();
	}

	private function dumpSess() {
		if (!Zend_Session::namespaceIsset(Macys_Constants::SECURE_DATA))
		echo "Secure Data Not Available";
		else {
			$x = new Zend_Session_Namespace(Macys_Constants::SECURE_DATA);
			if (!is_null($x->access_token))
			echo "</br>Access Token = " . $x->access_token;
			else
			echo "</br>Access Token Not Available";
			if (!is_null($x->refresh_token))
			echo "</br>Refrsh Token = " . $x->refresh_token;
			else
			echo "</br> Refresh Token Not Available ";
			if (!is_null($x->expires_in))
			echo " </br> expires in : " . $x->expires_in;
			else
			echo "</br> Expires In Not Available";
		}
	}


}